package mobileann.mafamily.utils;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.UUID;

import mobileann.mafamily.act.main.FS;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import com.mofind.android.base.L;

public class PhoneInfoUtils {
	public static String getPhoneNumber(Context cc) {
	    Context c = FS.getInstance();
		TelephonyManager tm = (TelephonyManager) c.getSystemService(Context.TELEPHONY_SERVICE);
		if(tm == null)
            return "";
		String number = tm.getLine1Number();
		return number == null ? "" : number;
	}

	public static String getIMEI(Context cc) {
	    Context c = FS.getInstance();
		TelephonyManager tm = (TelephonyManager) c.getSystemService(Context.TELEPHONY_SERVICE);
		if(tm == null)
			return "";
		String imei = tm.getDeviceId();
		return imei == null ? "" : imei;
	}

	public static String getIMSI(Context cc) {
	    Context c = FS.getInstance();
		TelephonyManager tm = (TelephonyManager) c.getSystemService(Context.TELEPHONY_SERVICE);
		if(tm == null)
			return "";
		String imsi = tm.getSubscriberId();
		return imsi == null ? "" : imsi;
	}

	/**
	 * 获取手机mac地址 错误返回12个0
	 */
	public static String getMacAddress(Context contextt) {
		// 获取mac地址：
	    Context context = FS.getInstance();
		String macAddress = "000000000000";
		try {
			WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
			WifiInfo info = (null == wifiMgr ? null : wifiMgr.getConnectionInfo());
			if (null != info) {
				if (!TextUtils.isEmpty(info.getMacAddress()))
					macAddress = info.getMacAddress().replace(":", "");
				else
					return macAddress;
			}
		} catch (Exception e) {
			L.e("maf", "error--", e);
			return macAddress;
		}
		
		return macAddress;
	}

	/**
	 * 获取手机型号
	 */
	public static String getPhoneModel() {
		return android.os.Build.MODEL;
	}

	/**
	 * 获取系统版本号
	 */
	public static String getRelease() {
		return Build.VERSION.RELEASE;
	}

	/**
	 * 获取SDK版本号
	 */
	public static String getSDK() {
		return Build.VERSION.SDK_INT + "";
	}
	
	public static int getSDKINT(){
		return Build.VERSION.SDK_INT;
	}

	/**
	 * 获取版本号
	 * 
	 * @return 当前应用的版本号
	 */
	public static String getVersion(Context cc) {
	    Context c = FS.getInstance();
		try {
			PackageManager manager = c.getPackageManager();
			PackageInfo info = manager.getPackageInfo(c.getPackageName(), 0);
			String version = info.versionName;
			return version;
		} catch (Exception e) {
			L.e("maf", "error--", e);
			return "2.0";
		}
	}

	/**
	 * 获取版本号 versioncode
	 * 
	 * @return 当前应用的版本号 versioncode
	 */
	public static String getVersionCode(Context cc) {
	    Context c = FS.getInstance();
		try {
			PackageManager manager = c.getPackageManager();
			PackageInfo info = manager.getPackageInfo(c.getPackageName(), 0);
			String version = String.valueOf(info.versionCode);
			return version;
		} catch (Exception e) {
			L.e("maf", "error--", e);
			return "110";
		}
	}
	/*
	 * 获取当前应用的内部版本号
	 */
	//	public static int getVersionCode(Context context){
	//		int inVersion=0;
	//		 PackageManager nPackageManager=context.getPackageManager();//得到包管理器
	//	        try {
	//	            PackageInfo nPackageInfo=nPackageManager
	//	                    .getPackageInfo(context.getPackageName(),PackageManager.GET_CONFIGURATIONS );
	//	            inVersion=nPackageInfo.versionCode;//得到现在app的版本号
	//	           
	//	        } catch (NameNotFoundException e1) {
	//	            // TODO Auto-generated catch block
	//	            e1.printStackTrace();
	//	        }
	//	        return inVersion;
	//	}
	public static String getMyUUID(Context cc) {
	    Context c = FS.getInstance();
		final TelephonyManager tm = (TelephonyManager) c.getSystemService(Context.TELEPHONY_SERVICE);
		if(tm == null)
            return "";
		final String tmDevice, tmSerial, tmPhone, androidId;
		tmDevice = "" + tm.getDeviceId();
		tmSerial = "" + tm.getSimSerialNumber();
		androidId = "" + getPhoneModel() + getRelease() + getSDK();
		UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());
		String uniqueId = deviceUuid.toString();		
		return uniqueId;
	}

	/**
	 * 获取渠道
	 * 
	 * @return 当前应用的渠道
	 */
	public static String getVersionChannel(Context cc) {
	    Context c = FS.getInstance();
		try {
			PackageManager manager = c.getPackageManager();
			ApplicationInfo info = manager.getApplicationInfo(c.getPackageName(), PackageManager.GET_META_DATA);
			String channel = String.valueOf(info.metaData.get("UMENG_CHANNEL"));
			return channel;
		} catch (Exception e) {
			L.e("maf", "error--", e);
			return "qijian";
		}
	}

	/**
	 * 获取当前IP地址
	 */
	public static String getLocalIpAddress() {
		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface intf = en.nextElement();
				for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
					InetAddress inetAddress = enumIpAddr.nextElement();
					if (!inetAddress.isLoopbackAddress()) {
						return inetAddress.getHostAddress().toString();
					}
				}
			}
		} catch (SocketException e) {
			L.e("maf", "error--", e);
		}
		return null;
	}
}
